#!/usr/bin/python

import time
import os
import shutil
from commands import getoutput

cwd = os.getcwd()
if not os.path.exists('.svn'):
    raise EnvironmentError('%s not a valid subversion root directory' % cwd)

DESTDIR = '%s/files/%s_%s' % ( os.environ['HOME'],
                               time.strftime('%m%d', time.localtime()),
                               os.path.basename(cwd) )

print DESTDIR

shutil.rmtree(DESTDIR, ignore_errors=True)
os.mkdir(DESTDIR)

SVNBASE = getoutput('svn info |grep ^URL |cut -d" " -f2')
print SVNBASE
open(DESTDIR + '/URL', 'w').write(SVNBASE)

SVNFILES = getoutput('svn status | egrep "^(A|M)"') #only add or modified
print SVNFILES
open(DESTDIR + '/FILES', 'w').write(SVNFILES)

for line in SVNFILES.split('\n'):
    status = line.split()[0]
    file = line.split()[-1]

    if os.path.isdir(file): continue

    dirname = os.path.dirname(file)
    destdir = '%s/%s' % (DESTDIR, dirname)

    if not os.path.exists(destdir):
        os.makedirs(destdir)

    shutil.copy(file, destdir)
